from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLineEdit, QPushButton, QWidget, QLabel
from func import view,thred
from config import button_config,input_config,frame_config
from datetime import  datetime

class MesUpdateSignal(QObject):
    data_updated = pyqtSignal(list)

class Chat_User(QDialog):
    def __init__(self,parent=None):
        super().__init__()

        self.mes_signal = MesUpdateSignal()
        self.mes_signal.data_updated.connect(self.update_mes_display)

        self.parent=parent
        self.client=self.parent.client
        self.recv_name=self.parent.recv_name
        self.recv_id=self.parent.recv_id
        self.user_id=self.parent.user_id
        self.user_name=self.parent.user_name
        self.stop_event=None
        title=f'{self.user_name}与{self.recv_name}的聊天窗口'
        self.setWindowTitle(title)
        self.resize(800, 600)
        main_layout = QVBoxLayout(self)
        parser={
            'user1' : self.recv_id,
            'user2' : self.user_id,
        }
        history_mes=self.client.get_chat(parser).get('data')
        self.stop_event = thred.star_thread(thred.mes, self.client, parser, history_mes, self.mes_signal)
        self.initUI(main_layout,history_mes)
    def initUI(self,main_layout,history_mes):
        frame=view.create_scroll(main_layout)
        self.layout1=QVBoxLayout(frame)
        self.insert_mes(history_mes)

        layout=QHBoxLayout()
        main_layout.addLayout(layout)
        self.send_input=QLineEdit()
        layout.addWidget(self.send_input)
        self.send_input.setStyleSheet(input_config.input_style5)
        self.send_input.setPlaceholderText('你要发送的消息')
        self.send_input.setFixedHeight(50)
        btn_send=QPushButton()
        btn_send.setStyleSheet(button_config.button_style2)
        btn_send.setFixedSize(100,50)
        btn_send.setText('发送')
        layout.addWidget(btn_send)
        btn_send.clicked.connect(self.mes_send)
        exit_btn=QPushButton()
        exit_btn.setStyleSheet(button_config.button_style4)
        exit_btn.setFixedSize(80,50)
        exit_btn.setText('返回')
        layout.addWidget(exit_btn)
        exit_btn.clicked.connect(self.exit)

    def insert_mes(self,history):
        for data in history:
            layout2=QHBoxLayout()
            self.layout1.addLayout(layout2)
            label=QLineEdit()
            label.setReadOnly(True)
            label.setFixedHeight(100)
            if data[0]==self.user_id:
                text=f'({data[2]}){self.user_name}:{data[3]}'
                style=input_config.input_style2
            else:
                text=f'({data[2]}){self.recv_name}:{data[3]}'
                style=input_config.input_style4
            label.setText(text)
            label.setStyleSheet(style)
            layout2.addWidget(label)
    def mes_send(self):
        parser={
            'sender_id' : self.user_id,
            'receiver_id' : self.recv_id,
            'message' : self.send_input.text(),
            'datetime':datetime.now().strftime('%Y-%m-%d %H:%M')
        }
        self.client.send_chat(parser)

    def update_mes_display(self,data):
        self.insert_mes(data)

    def exit(self):
        self.close()
        self.accept()

    def accept(self):
        super().accept()

    def closeEvent(self,event):
        if self.stop_event != None:
            self.stop_event.set()





